﻿using System;
using System.Web.UI.WebControls;
using CvModel;
using DAL;

namespace LAB3_Cv
{
    public partial class CompanyAds : System.Web.UI.Page
    {
        private ContactPerson _contact;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["AdminId"] != null)
                {
                    _contact = Session["AdminId"] as ContactPerson;
                    int adminId = _contact.WorkAdminId; 
                    GetAllAdsFromWorkAdminId(adminId);
                    LabelCompanyName.Text = AdminDB.GetAdminOnID(adminId).CompanyName;
                    Session["AdminId"] = null;
                }
            }
        }
        public void GetAllAdsFromWorkAdminId(int adminId)
        {
            ListViewAds.DataSource = WorkAdDB.GetAllAdsFromAdminId(adminId);
            ListViewAds.DataBind();
        }

        protected void ListViewAds_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            try
            {
                var listViewItem = e.Item as ListViewItem;

                var articleId = (int)ListViewAds.DataKeys[listViewItem.DataItemIndex].Value;
                Session["WorkAdId"] = articleId;
                Response.Redirect("WorkAd.aspx");
            }
            catch (Exception ex)
            {
                LabelException.Text = ex.Message;
            }
        }
    }
}